package com.markyao.service.raft;

import com.markyao.recos.regist.RecosRegistConfig;
import com.markyao.utils.EncoderUtils;
import com.markyao.vo.Result;
import com.markyao.vo.pojo.ConfigInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import javax.servlet.http.HttpServletRequest;
import java.net.URI;
import java.util.Map;

@Component
@Slf4j(topic = "m.BroadcastService")
public class BroadcastService {
    @Autowired
    RecosRegistConfig recosRegistConfig;
    @Autowired
    RestTemplate restTemplate;


    protected boolean generalBroadcast(String route,String errorLog){
        if (!recosRegistConfig.isGroup() || !recosRegistConfig.isLeader()){
            log.error("Not group | Not leader!!");
            return false;
        }
        String[] groupAds = recosRegistConfig.getGroupAds();
        for (String ads : groupAds) {
            MultiValueMap<String, String> headrs = getTokenHeadersMap(ads);
            if (headrs==null){
                log.error("与目标recos尚未建立连接! {}",ads);
                continue;
            }
            HttpEntity httpEntity=new HttpEntity(headrs);
            try {
                ResponseEntity<Result> responseEntity =
                        restTemplate.exchange(ads + route, HttpMethod.GET, httpEntity, Result.class);
                if (responseEntity!=null && responseEntity.getBody()!=null && responseEntity.getBody().getSuccess()){
                    continue;
                }
            } catch (RestClientException e) {
                log.error(errorLog,ads);
                e.printStackTrace();
            }
            return false;
        }
        return true;

    }
    protected boolean generalBroadcastPost(String route,String errorLog,Object requestBody){
        if (!recosRegistConfig.isGroup() || !recosRegistConfig.isLeader()){
            log.error("Not group | Not leader!!");
            return false;
        }
        String[] groupAds = recosRegistConfig.getGroupAds();
        for (String ads : groupAds) {
            MultiValueMap<String, String> headrs = getTokenHeadersMap(ads);
            if (headrs==null){
                log.error("与目标recos尚未建立连接! {}",ads);
                continue;
            }
            HttpEntity httpEntity=new HttpEntity(requestBody,headrs);
            try {
                ResponseEntity<Result> responseEntity =
                        restTemplate.exchange(ads + route, HttpMethod.POST, httpEntity, Result.class);
                if (responseEntity!=null && responseEntity.getBody()!=null && responseEntity.getBody().getSuccess()){
                    continue;
                }
            } catch (RestClientException e) {
                log.error(errorLog,ads);
                e.printStackTrace();
            }
            return false;
        }
        return true;

    }

    protected MultiValueMap<String, String> getTokenHeadersMap(String ads) {
        if (!recosRegistConfig.getTokenMap().containsKey(ads)){
            return null;
        }
        MultiValueMap<String,String>headrs=new LinkedMultiValueMap<>();
        headrs.add("Authorization",recosRegistConfig.getTokenMap().get(ads));
        return headrs;
    }



}
